package servlet.ware;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Goods;
import model.Orders;
import service.GoodsService;

/**
 * Servlet implementation class goods_buy_Servlet
 */
@WebServlet("/goods_buy")
public class ware_buy_Servlet extends HttpServlet {
	
	private  GoodsService gService  = new GoodsService();
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
//		Map<String, String[]> map = request.getParameterMap();
//		for(String key : map.keySet()) {
//			
//			System.out.println(key+":"+map.get(key));
//			
//		}
		
		Orders o = null;
		if(request.getSession().getAttribute("orders") != null) {
			
			o =(Orders) request.getSession().getAttribute("orders");
			
		}else {
			
			o = new  Orders();
			request.getSession().setAttribute("orders", o);
			
		}
		
		int goodsid = Integer.parseInt(request.getParameter("goodsid"));
		Goods goods  = gService.getById(goodsid);
		if(goods.getStock()>o.getAmount()) {
			
			o.addGoods(goods);
			response.getWriter().print("ok");
			
		}else {
			
			response.getWriter().print("fail");
		}
		
	}

}
